package flexconsole.view {
	import flexconsole.AppFacade;
	import flexconsole.ConfigKeys;
	import flexconsole.model.IConfigProxy;
	import flexconsole.view.component.Toolbar;
	
	import flexunit.framework.Assert;
	
	import mx.logging.ILogger;
	import mx.logging.Log;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class ToolbarMediator extends Mediator {
		
		static private var logger:ILogger = Log.getLogger("flexconsole.view.ToolbarMediator");
		
		private var isViewDirty:Boolean = false;
		
		public function ToolbarMediator(mediatorName:String = null, viewComponent:Object = null) {
			super(mediatorName, viewComponent);
		}
		
		public function get view():Toolbar {
			return viewComponent as Toolbar;
		}
		
		public function get appFacade():AppFacade {
			return facade as AppFacade;
		}
		
		override public function onRegister():void {
			logger.debug("onRegister()");
			super.onRegister();
			Assert.assertNotNull(appFacade);
			Assert.assertNotNull(view);
			view.resourceBundle = appFacade.resourceBundle;
			updateView();
		}
		
		override public function listNotificationInterests():Array {
			return [ AppFacade.DATA_CHANGE ];
		}
		
		override public function handleNotification(notification:INotification):void {
			if (AppFacade.PROXYNAME_CONFIG == notification.getType()) {
				if (notification.getBody() == null) {
					// full refresh
					invalidateView();
				} else {
					// watch key changes
					switch (notification.getBody()) {
						case ConfigKeys.FILTER_KEYWORD:
						case ConfigKeys.FILTER_LOG_LEVELS:
						case ConfigKeys.FILTER_VISIBLE:
							invalidateView();
							break;
					}
				}
			}
		}
		
		protected function invalidateView():void {
			if (view && !isViewDirty) {
				isViewDirty = true;
				view.callLater(updateView);
			}
		}
		
		protected function updateView():void {
			logger.debug("updateView()");
			isViewDirty = false;
			var configProxy:IConfigProxy = appFacade.configProxy;
			view.filterVisible = configProxy.getValue(ConfigKeys.FILTER_VISIBLE, true);
			view.connected = configProxy.getValue(ConfigKeys.CONNECTED, false);
			view.keyword = configProxy.getValue(ConfigKeys.FILTER_KEYWORD, "");
		}
	
	
	}
}

